﻿Public Class frmUsers
    Private szSQL As String
    Private rs As ADODB.Recordset
    Private m_SortingColumn As ColumnHeader
    Private m_SortingIndex As Integer = 0
    Dim OLD_CODE As String

    Private Sub frmUsers_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        InitListeUsers(0)
        InitComboAgences(CB_Agences)

    End Sub

    Private Sub InitListeUsers(ByVal ix As Integer)

        Dim nb As Integer = 0

        ' si la colonne de tri est différente de celle par défaut, retrier la listview sur la 1ere colonne
        If m_SortingIndex > 0 Then
            SortListView(LV_USERS, m_SortingColumn, 0)
        End If
        ' effacer la listview
        LV_USERS.Items.Clear()
        LV_USERS.ListViewItemSorter = Nothing

        ' Lire la table FORMULES
        szSQL = "SELECT cd_uti, nom, secu from P_USERS ORDER BY cd_uti"
        GetRecordset(ctDSN_AEM, szSQL, rs)

        ' Load the data.
        Do While Not rs.EOF
            LV_USERS.Items.Add(rs.Fields("cd_uti").Value)
            LV_USERS.Items(nb).SubItems.Add(rs.Fields("nom").Value)
            LV_USERS.Items(nb).SubItems.Add(rs.Fields("secu").Value)

            ' Get the next record.
            rs.MoveNext()
            nb = nb + 1
        Loop
        ' Close the recordset AND connection.
        rs.Close()

        LB_NbLignes.Text = CStr(nb) & " lignes"
        ' restaurer l'ancienne ordre de tri de la listview
        If m_SortingIndex > 0 Then
            SortListView(LV_USERS, m_SortingColumn, m_SortingIndex)
        End If
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_USERS)

    End Sub

    Private Sub LV_USERS_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles LV_USERS.ColumnClick
        SortListView(LV_USERS, m_SortingColumn, e.Column)
        m_SortingIndex = e.Column
    End Sub

    Private Sub LV_USERS_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles LV_USERS.DoubleClick

        If LV_USERS.SelectedItems.Count > 0 Then
            UsertoScreen(LV_USERS.SelectedItems(0).Text)
            ' afficher les détails de la formule
            tb_details.Visible = True
            TE_CODE.Focus()
            BO_ENREGISTRER.Visible = True
            ' mémoriser ancien identifiant pour maj
            OLD_CODE = TE_CODE.Text
        End If

    End Sub

    Private Sub LV_USERS_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles LV_USERS.KeyDown
        Select Case e.KeyCode
            ' touche Enter = DoubleClick
            Case Keys.Enter
                LV_USERS_DoubleClick(sender, e)

            Case Keys.Insert
                BO_ADD_LIGNE_Click(sender, e)

            Case Keys.Delete
                BO_DELETE_LIGNE_Click(sender, e)

        End Select

    End Sub

    Private Sub UsertoScreen(ByVal cd_uti As String)

        ' Lire l'enreg sélectionné
        szSQL = "SELECT * FROM P_USERS WHERE cd_uti = '" & cd_uti & "'"
        GetRecordset(ctDSN_AEM, szSQL, rs)
        If rs.RecordCount > 0 Then
            TE_CODE.Text = rs.Fields("cd_uti").Value
            TE_NOM.Text = rs.Fields("nom").Value
            TE_PSW.Text = rs.Fields("psw").Value
            TE_SECU.Value = rs.Fields("secu").Value
            CB_Agences.SelectedIndex = rs.Fields("agence").Value - 1
            TE_MODIFIE_LE.Text = setModifiedBy(rs.Fields("cree_le").Value, rs.Fields("modif_le").Value, rs.Fields("cd_uti").Value)
        End If
        rs.Close()

    End Sub

    Private Sub BO_Enregister_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_ENREGISTRER.Click

        ' mise à jour de l'utilisateur
        szSQL = "CALL spUpd_USERS('" & TE_CODE.Text & "','" & nq(TE_NOM.Text) & _
            "','" & TE_PSW.Text & "'," & TE_SECU.Value & "," & CB_Agences.SelectedIndex + 1 & ",'" & OLD_CODE & "')"
        ExecQuery(ctDSN_AEM, szSQL)
        ' Cacher les détails
        BO_ANNULER_Click(sender,e)

    End Sub

    Private Sub BO_ANNULER_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_ANNULER.Click
        ' Cacher les détails
        tb_details.Visible = False
        ' réaffiche la viewlist
        InitListeUsers(LV_USERS.SelectedItems(0).Index)
    End Sub

    Private Sub BO_DELETE_LIGNE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_DELETE_LIGNE.Click
        deleteEnregistrement("P_USERS", LV_USERS.SelectedItems(0).SubItems(1).Text, LV_USERS.SelectedItems(0).SubItems(0).Text)
        ' Cacher les détails
        BO_ANNULER_Click(sender, e)
    End Sub

    Private Sub BO_ADD_LIGNE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_ADD_LIGNE.Click
        ' Ajouter un nouvel utilisateur
        szSQL = "CALL spINS_USERS('000')"
        If ExecQuery(ctDSN_AEM, szSQL) Then
            ' réaffiche la viewlist
            InitListeUsers(0)
            ' ouvrir le fomulaire de modif
            LV_USERS_DoubleClick(sender, e)
        End If
    End Sub
End Class